package com.lyrcsoft.rxjava1x.operators.filter;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fujc2dev@126.com
 * @date 2024-11-21
 */
public class distinct {
    /**
     * distinct()的过滤规则是只允许还没有发射过的数据通过，所有重复的数据项都只会发射一次。
     *
     * @param args
     */
    public static void main(String[] args) {

        Observable.just(2, 1, 2, 2, 3, 4, 3, 4, 5, 5)
                .distinct()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        System.out.print(i + " ");
                    }
                });

        System.out.println("");
        System.out.println("==============================================================");
        //House构造函数中的第一个参数为该房源所属小区名，第二个参数为房源描述
        // distinct(Func1)参数中的Func1中的call方法会根据Observable发射的值生成一个Key，
        // 然后比较这个key来判断两个数据是不是相同；
        // 如果判定为重复则会和distinct()一样过滤掉重复的数据项。
        List<House> houses = new ArrayList<>();
        houses.add(new House("中粮·海景壹号", "中粮海景壹号新出大平层！总价4500W起"));
        houses.add(new House("竹园新村", "满五唯一，黄金地段"));
        houses.add(new House("竹园新村", "一楼自带小花园"));
        houses.add(new House("中粮·海景壹号", "毗邻汤臣一品"));
        houses.add(new House("中粮·海景壹号", "顶级住宅，给您总统般尊贵体验"));
        houses.add(new House("竹园新村", "顶层户型，两室一厅"));
        houses.add(new House("中粮·海景壹号", "南北通透，豪华五房"));
        Observable.from(houses)
                .distinct(new Func1<House, String>() {

                    @Override
                    public String call(House house) {
                        return house.communityName;
                    }
                })
                .subscribe(new Action1<House>() {
                    @Override
                    public void call(House house) {
                        System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc);
                    }
                });
    }
}

class House {

    public String communityName;
    public String desc;

    public House(String communityName, String desc) {
        this.communityName = communityName;
        this.desc = desc;
    }

}

